package com.renren.questions.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.renren.commons.result.ResultResponse;
import com.renren.entity.dto.ElementAdd;
import com.renren.entity.dto.TopicId;
import com.renren.entity.dto.Topics;
import com.renren.entity.po.TbElement;
import com.renren.entity.po.TbTopic;
import com.renren.questions.mapper.TbElementMapper;
import com.renren.questions.mapper.TopicDao;

import com.renren.questions.service.TbTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author csh 1161239152@qq.com
 * @since 1.0.0 2022-09-17
 */
@Service
public class TbTopicServiceImpl implements TbTopicService {
    @Autowired
    TopicDao topicDao;
    @Autowired
    TbElementMapper elementDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public ResultResponse findPage(Topics topics) {
//        QueryWrapper<TbTopic> queryWrapper=new QueryWrapper<>();
//        queryWrapper.lambda().eq(TbTopic::getTopicElement,topics.getTopicElement());
//
//        List<TbTopic> topicEntities = topicDao.selectList(
//                new QueryWrapper<TbTopic>().lambda().eq(
//                        TbTopic::getElementId, topics.getElementId()
//                )
//        );
        //分单选，多选，判断查询出来
//        QueryWrapper<TbTopic> queryWrapper=new QueryWrapper<>();
//        queryWrapper.lambda().eq( TbTopic::getElementId, topics.getElementId());
//        queryWrapper.lambda().eq(TbTopic::getTypeId,2);
        List<TbTopic> topicEntities1 = topicDao.selectList(
                new QueryWrapper<TbTopic>().lambda().eq(
                        TbTopic::getElementId, topics.getElementId()
                ).eq(TbTopic::getTypeId, 1)

        );
        List<TbTopic> topicEntities2 = topicDao.selectList(
                new QueryWrapper<TbTopic>().lambda().eq(
                        TbTopic::getElementId, topics.getElementId()
                ).eq(TbTopic::getTypeId, 2)
        );
        //判断
        List<TbTopic> topicEntities3 = topicDao.selectList(
                new QueryWrapper<TbTopic>().lambda().eq(
                        TbTopic::getElementId, topics.getElementId()
                ).eq(TbTopic::getTypeId, 3)
        );
        Map<String,Object> map=new HashMap();
        map.put("topicList1",topicEntities1);
        map.put("topicList2",topicEntities2);
        map.put("topicList3",topicEntities3);
        redisTemplate.opsForHash().put("map","map"+topics.getElementId(),map);

        return ResultResponse.SUCCESS(map);
    }

    @Override
    public ResultResponse findElement() {
        List<TbElement> elementEntities = elementDao.selectList(null);
        return ResultResponse.SUCCESS(elementEntities);
    }

    @Override
    public ResultResponse findTopicAdd(TbTopic topicElement) {
        //倒叙查询 这个单元的题目
        QueryWrapper<TbTopic> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TbTopic::getElementId,topicElement.getElementId());
        queryWrapper.lambda().orderByDesc(TbTopic::getCreatTime);

        List<TbTopic> topicEntities = topicDao.selectList(queryWrapper);
        Integer topicId=0;
        if(topicEntities.size()<=0){

        }else{
            TbTopic tbTopic = topicEntities.get(0);
             topicId= tbTopic.getTopicId();
        }

        //我们拿到做后一个添加的题目 可以找到他的题目id
        topicElement.setTopicId(topicId.intValue()+1);
        topicElement.setCreatTime(new Date());
        topicElement.setDeletes(0);
        //添加题目信息
        topicDao.insert(topicElement);


        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse findElementAdd(ElementAdd elementEntity) {
        TbElement elementEntity1=new TbElement();
        elementEntity1.setElementName(elementEntity.getElementName());
        elementDao.insert(elementEntity1);
        return ResultResponse.FAILED(200,"添加成功");
    }

    @Override
    public ResultResponse findTopicUpdate(TbTopic topicElement) {
        topicDao.updateById(topicElement);

        return ResultResponse.FAILED(200,"修改成功");
    }

    @Override
    public ResultResponse findTopicDelete(TopicId topicId) {
        topicDao.deleteById(topicId.getId());
        return ResultResponse.FAILED(200,"删除成功");
    }
}


